package inits

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"log"
	"zk333/config"
	"zk333/globals"
	"zk333/models/database"
)

func InitMysql() {
	mData := config.DataConfig.Mysql
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",
		mData.Username,
		mData.Password,
		mData.Host,
		mData.Port,
		mData.Database,
	)
	var err error
	globals.DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic(err)
	}
	log.Println("Mysql Database Connect Success!")
	// 迁移数据表
	err = globals.DB.AutoMigrate(&database.Post{}, &database.Like{}, &database.Comment{})
	if err != nil {
		panic(err)
	}
}
